package com.lrs.java.j2se_self.thread;

public class TestSync implements Runnable {
	Timer timer = new Timer();
	
	public static void main(String[] args){
		TestSync ts = new TestSync();
		Thread t1 = new Thread(ts);
		Thread t2 = new Thread(ts);
		t1.setName("t1");
		t2.setName("t2");
		t1.start();
		t2.start();
	}
	public void run(){
		timer.add(Thread.currentThread().getName());
	}
}
class Timer {
	public static int num = 0;
	
	public synchronized void add(String s){
		//synchronized(this){
			num++;
			try{
				Thread.sleep(1000);
			}catch(InterruptedException e){
			
			}
			System.out.println(s + ":是第"+num+"访问对象");
		//}
	}
}